package com.glela.api.pay.yeepay;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glela.common.util.HttpUtil;
import com.glela.common.util.YeePayAESUtil;
import com.glela.common.util.YeePayDigest;

public class YeePayDemo {
	
	public static void main(String[] args) throws IOException {
		String result = "";
		
		// 查询
		String data = getYeePayRefundString();
		String url = "https://o2o.yeepay.com/zgt-api/api/queryBindCards";
		
		// 删除
		/*String data = deleteYeePayRefundString();
		String url = "https://o2o.yeepay.com/zgt-api/api/unbindCard";*/
		
		String content = "customernumber=" + "10013842859" + "&data=" + data;
		result = HttpUtil.doPost(content, url, "application/x-www-form-urlencoded");
		String realResult = YeePayAESUtil.decrypt(JSONObject.parseObject(result).get("data").toString(), "lF0386j067MWZb7817003M8o32iW7918H9Fk3JRV758P10lp182T7B8zte2u".substring(0, 16));
		System.out.println(realResult);
	}
	
	/**
	 * 查询绑卡列表
	 * https://o2o.yeepay.com/zgt-api/api/queryBindCards
	 * {"bindcardlist":[{"bankCardType":"DEBITCARD","bankCode":"CCB","bankName":"建设银行","bindId":54292153,"cardNo":"0554","idcard":"0611","invalidTime":"2038-01-01 00:00:00","mobile":"188****0700"}],"code":"1","customernumber":"10013842859","hmac":"b3ff4626e7c1127bf7e88c940f01fca1","userno":"152"}
	 * @return
	 */
	protected static String getYeePayRefundString() {
		// TODO Auto-generated method stub
		
		String customernumber = "10013842859"; //
		String userno = "152"; // 退款请求号   商户传入，必须在该商编下唯一
		
		String keyForAes = "lF0386j067MWZb7817003M8o32iW7918H9Fk3JRV758P10lp182T7B8zte2u".substring(0, 16);
		String[] stringArray = { customernumber, userno };
		
		String hmac = YeePayDigest.getHmac(stringArray, "lF0386j067MWZb7817003M8o32iW7918H9Fk3JRV758P10lp182T7B8zte2u");
		
		Map<String, String> dataMap = new HashMap<String, String>();
		dataMap.put("customernumber", customernumber);
		dataMap.put("userno", userno);
		dataMap.put("bindid ", "");
		dataMap.put("hmac", hmac);
		
		String dataJsonString = JSON.toJSONString(dataMap); // 生成data
		// AES加密
		String data = YeePayAESUtil.encrypt(dataJsonString, keyForAes);
		return data;
	}
	
	/**
	 * 解绑
	 * https://o2o.yeepay.com/zgt-api/api/unbindCard
	 * @return
	 */
	protected static String deleteYeePayRefundString() {
		// TODO Auto-generated method stub
		
		String customernumber = "10013842859"; //
		String userno = "152"; // 退款请求号   商户传入，必须在该商编下唯一
		String bindid = "54292153"; // 54292153
		String cause = "解除测试卡片";
		
		String keyForAes = "lF0386j067MWZb7817003M8o32iW7918H9Fk3JRV758P10lp182T7B8zte2u".substring(0, 16);
		String[] stringArray = { customernumber, userno, bindid };
		
		String hmac = YeePayDigest.getHmac(stringArray, "lF0386j067MWZb7817003M8o32iW7918H9Fk3JRV758P10lp182T7B8zte2u");
		
		Map<String, String> dataMap = new HashMap<String, String>();
		dataMap.put("customernumber", customernumber);
		dataMap.put("userno", userno);
		dataMap.put("bindid", bindid);
		dataMap.put("cause", cause);
		dataMap.put("hmac", hmac);
		
		String dataJsonString = JSON.toJSONString(dataMap); // 生成data
		// AES加密
		String data = YeePayAESUtil.encrypt(dataJsonString, keyForAes);
		return data;
	}

}
